package com.nanyang.tms.service.receipt.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.nanyang.common.core.annotation.SystemLog;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ExceptionProcessorUtils;
import com.nanyang.common.core.exception.ServiceException;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.tms.api.dto.request.receipt.*;
import com.nanyang.tms.api.dto.response.receipt.*;
import com.nanyang.tms.api.service.receipt.ReceiptBillRpcService;
import com.nanyang.tms.dao.receipt.ReceiptBillDao;
import com.nanyang.tms.dao.receipt.ReceiptRelationDao;
import com.nanyang.tms.domain.receipt.ReceiptBillDO;
import com.nanyang.tms.domain.receipt.ReceiptBillQuery;
import com.nanyang.tms.domain.receipt.ReceiptBillVO;
import com.nanyang.tms.domain.receipt.ReceiptRelationVO;
import com.nanyang.tms.manager.receipt.ReceiptBillManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service(version = "1.0.0", timeout = 10000, retries = 0, validation = "true")
public class ReceiptBillRpcServiceImpl implements ReceiptBillRpcService {
    @Autowired
    private ReceiptBillManager receiptBillManager;
    @Autowired
    private ReceiptBillDao receiptBillDao;
    @Autowired
    private ReceiptRelationDao receiptRelationDao;
    @Override
    public ApiResult<PageBean<ReceiptBillPageResponse>> listReceiptBill(ReceiptBillQueryRequest param) {
        ApiResult<PageBean<ReceiptBillPageResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            ReceiptBillQuery query = new ReceiptBillQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<ReceiptBillVO> receiptList = receiptBillManager.listReceiptBill(query);
            PageBean<ReceiptBillPageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(receiptList.getTotalItem());
            BeanUtils.copyListProperties(receiptList.getData(), resultList.getData(), ReceiptBillPageResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }
    @Override
    public ApiResult<PageBean<ReceiptBillFuzzyPageResponse>> listFuzzyReceiptBill(ReceiptBillFuzzyQueryRequest param) {
        ApiResult<PageBean<ReceiptBillFuzzyPageResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            ReceiptBillQuery query = new ReceiptBillQuery();
            BeanUtils.copyProperties(param, query);
            query.setState(0);
            PageBean<ReceiptBillVO> receiptList = receiptBillManager.listReceiptBill(query);
            PageBean<ReceiptBillFuzzyPageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(receiptList.getTotalItem());
            BeanUtils.copyListProperties(receiptList.getData(), resultList.getData(), ReceiptBillFuzzyPageResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }
    @Override
    public ApiResult<List<ReceiptBillDetailResponse>> selectByCode(String receiptCode) {
        ApiResult<List<ReceiptBillDetailResponse>> result = new ApiResult<>();
        try {
            List<ReceiptBillDetailResponse> receiptBillDetailResponseList = new ArrayList<>();
            List<ReceiptRelationVO> receiptRelationVOS = receiptRelationDao.selectByCode(receiptCode,null);
            if(CollectionUtil.isNotEmpty(receiptRelationVOS)){
                BeanUtils.copyListProperties(receiptRelationVOS, receiptBillDetailResponseList, ReceiptBillDetailResponse.class);
            }
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData( receiptBillDetailResponseList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> saveReceiptBill(ReceiptBillAddRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            ReceiptBillDO receiptDo = new  ReceiptBillDO();
            BeanUtils.copyProperties(param,receiptDo);
            receiptDo.setGmtCreatePin(param.getPin());
            receiptDo.setReceiptTime(DateUtil.parseDateTime(param.getReceiptTime()));
            receiptBillManager.saveReceiptBill(receiptDo);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
    @SystemLog(module =SystemLog.Module.WAYBILL,template="receiptBillState")
    @Override
    public ApiResult<Boolean> updateState(ReceiptBillUpdateStateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            ReceiptBillDO receiptDo = new  ReceiptBillDO();
            BeanUtils.copyProperties(param,receiptDo);
            receiptDo.setGmtModifiedPin(param.getPin());
            receiptBillDao.updateState(receiptDo);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> cancelState(ReceiptBillUpdateStateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            ReceiptBillDO receiptDo = new  ReceiptBillDO();
            BeanUtils.copyProperties(param,receiptDo);
            receiptDo.setGmtModifiedPin(param.getPin());
            receiptBillManager.cancelState(receiptDo);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<PageBean<ReceiptBillFrontPageResponse>> listFrontReceiptBill(ReceiptBillCustomerQueryRequest param) {
        ApiResult<PageBean<ReceiptBillFrontPageResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            ReceiptBillQuery query = new ReceiptBillQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<ReceiptBillVO> receiptList = receiptBillManager.listReceiptBill(query);
            PageBean<ReceiptBillFrontPageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(receiptList.getTotalItem());
            BeanUtils.copyListProperties(receiptList.getData(), resultList.getData(), ReceiptBillFrontPageResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }

    @Override
    public ApiResult<List<ReceiptBillFrontDetailResponse>> selectFrontByCode(ReceiptBillFrontDetailRequest param) {
        ApiResult<List<ReceiptBillFrontDetailResponse>> result = new ApiResult<>();
        try {
            List<ReceiptBillFrontDetailResponse> receiptBillDetailResponseList = new ArrayList<>();
            List<ReceiptRelationVO> receiptRelationVOS = receiptRelationDao.selectByCode(param.getReceiptCode(),param.getCompanyCode());
            if(CollectionUtil.isNotEmpty(receiptRelationVOS)){
                BeanUtils.copyListProperties(receiptRelationVOS, receiptBillDetailResponseList, ReceiptBillFrontDetailResponse.class);
            }
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData( receiptBillDetailResponseList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
}
